import {fakeAccountLogin, fakeMobileLogin, fakeAccountLogout, checkToekn} from '../services/api';

export default {
  namespace: 'login',

  state: {
    status: undefined,
  },

  effects: {
    *accountSubmit({ payload }, { call, put }) {
      yield put({
        type: 'changeSubmitting',
        payload: true,
      });
      const response = yield call(fakeAccountLogin, payload);
      yield put({
        type: 'loginHandle',
        payload: response,
      });
      yield put({
        type: 'changeSubmitting',
        payload: false,
      });
    },
    *mobileSubmit(_, { call, put }) {
      yield put({
        type: 'changeSubmitting',
        payload: true,
      });
      const response = yield call(fakeMobileLogin);
      yield put({
        type: 'loginHandle',
        payload: response,
      });
      yield put({
        type: 'changeSubmitting',
        payload: false,
      });
    },
    *checkToken({ payload }, { call, put }){
        yield put({
            type: 'changeSubmitting',
            payload: true,
        });
        const response = yield call(checkToekn, payload);
        yield put({
            type: 'checkToekn',
            payload: response,
        });
        yield put({
            type: 'changeSubmitting',
            payload: false,
        });
    },
    *logout({ payload, callback }, { call, put }) {
        const userInfoState = localStorage.getItem('userInfoState');
        const token = localStorage.getItem('token');
        if (userInfoState||token) {
            const response = yield call(fakeAccountLogout, payload);
            // 如果本地存在 userInfoState 将其移除
            localStorage.removeItem('userInfoState');
            localStorage.removeItem('token');
            localStorage.removeItem('menu');
        }
      yield put({
        type: 'logoutHandle',
        payload,
      });
      if (callback) {
        callback();
      }
    },
  },

  reducers: {
    loginHandle(state, { payload }) {
      if(payload.result===0){
          const token = localStorage.getItem('token');
          if (token) {
              // 如果本地存在 userInfoState 将其移除
              localStorage.removeItem('token');
              localStorage.removeItem('userInfoState');
              localStorage.removeItem('menu');
              sessionStorage.removeItem('userInfoState');
              sessionStorage.removeItem('token');
              sessionStorage.removeItem('menu');
          }
          const userInfoJson=JSON.stringify({
              userInfo:payload["resultData"]["userInfo"],
              actionCodeList:payload["resultData"]["actionCodeList"],
              token:payload["token"],
              timestamp: new Date().getTime(),
          });
          localStorage.setItem('userInfoState', userInfoJson);
          localStorage.setItem('token',payload["token"]);
          localStorage.setItem('menu',JSON.stringify(payload["resultData"]["adminMenu"]))
          sessionStorage.setItem('menu',JSON.stringify(payload["resultData"]["adminMenu"]))
          sessionStorage.setItem('token',payload["token"]);
          sessionStorage.setItem('userInfoState', userInfoJson);

      }
      return {
        ...state,
        status: payload.result,
        type: payload.privates.type,
      };
    },
    checkToekn(state, { payload }){
        if(payload.result===0){
            sessionStorage.removeItem('userInfoState');
            sessionStorage.removeItem('token');
            sessionStorage.setItem('token',payload["token"]);
            sessionStorage.setItem('userInfoState', JSON.stringify({
                userInfo:payload["resultData"]["userInfo"],
                token:payload["token"],
                timestamp: new Date().getTime()
            }));
        }else{
            localStorage.removeItem('token');
            localStorage.removeItem('userInfoState');
        }
        return {
            ...state,
            status: payload.result,
            isCurrSession: true
        };
    },
    changeSubmitting(state, { payload }) {
      return {
        ...state,
        submitting: payload,
      };
    },
    logoutHandle(state, { payload }) {
      return {
        ...state,
        status: payload.status,
      };
    },
  },
};
